package core.sceneManager.sceneSection.scene2DSection
{
	import ca.turbulent.media.Pyro;
	import ca.turbulent.media.events.PyroEvent;
	
	import core.MainManager;
	import core.sceneManager.sceneSection.AnimateSection;
	import core.sceneManager.sceneSection.ITipBehavior;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import memory.MemoryRecovery;
	/**
	 * 2D场景中的FLV模块,等待创建完毕的方式有三种,waitForReady,waitForComplete,waitPlayComplete
	 */ 
	public class FlvSection extends AnimateSection implements ITipBehavior
	{
		private var _mouseClickScript:String;                          //鼠标离开所要执行的脚本
		public var urlClear:String="";                                 //流畅版视频的路径
		public var urlSmooth:String="";                                //清晰版视频的路径
		private var _completeScript:String="";                         //播放完毕的时候，所要执行的脚本
		private var _readyScript:String="";                            //准备完毕的时候，所要执行的脚本
		private var pyro:Pyro;                                         //视频
		public function FlvSection(target:IEventDispatcher=null)
		{
			super(target);
			pyro=new Pyro();
		}
		override public function start():void
		{
			if(MainManager.QUALITY==MainManager.CLEAR)
			{
				pyro.play(this.urlClear);
			}else
			{
				pyro.play(this.urlSmooth);
			}
		}
		override public function excute(xml:XML, container:*):void
		{
			if(container==null) throw new Error("容器不能为空!");
			this.dataBase=xml;
			for each(var item:XML in xml.attributes())
			{
				var key:String=item.name();
				try
				{
					this[key]=item.toString();
				}catch(e:Error)
				{
					try
					{
						pyro[key]=item.toString();
					}catch(e:Error)
					{
						
					}
				}
			}
			container.addChild(pyro);
		}
		override public function resume():void
		{
			pyro.togglePause();
		}
		override public function pause():void
		{
			pyro.pause();
		}
		override public function dispose():void
		{
			super.dispose();
			MemoryRecovery.getInstance().gcFun(this,MouseEvent.CLICK,on_mouse_click);
			MemoryRecovery.getInstance().gcFun(pyro,PyroEvent.BUFFER_FULL,on_buffered);
			MemoryRecovery.getInstance().gcFun(pyro,Event.ENTER_FRAME,onEnter);
			MemoryRecovery.getInstance().gcFun(pyro,PyroEvent.COMPLETED,on_play_complete);
			MemoryRecovery.getInstance().gcFun(pyro,PyroEvent.COMPLETED,on_play_complete);
			MemoryRecovery.getInstance().gcObj(pyro);
			pyro=null;
			_mouseClickScript=null;
			urlClear=null;
			urlSmooth=null;
			_completeScript=null;
			_readyScript=null;
		}
		////////////////////////////////////////////设置属性给XML配置/////////////////////
		/**
		 * 注册鼠标点击时的行为
		 */ 
		public function set mouseClickScript(val:String):void
		{
			if(val==null || val=="") return;
			_mouseClickScript=val;
			pyro.buttonMode=true;
			pyro.useHandCursor=true;
			pyro.addEventListener(MouseEvent.CLICK,on_mouse_click);
		}
		private function on_mouse_click(e:MouseEvent):void
		{
			MainManager.getInstance().scriptManager.runScriptDirectly(_mouseClickScript);
		}
		public function set chineseTip(val:String):void
		{
		}
		
		public function set englishTip(val:String):void
		{
		}
		//是否等待加载完毕
		private function set waitForComplete(val:int):void
		{
			if(val==1)
			{
				pyro.addEventListener(PyroEvent.BUFFER_FULL,on_buffered);
			}
		}
		private function on_buffered(e:PyroEvent):void
		{
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		 //设置在界面中的层次 ,默认是1层，也就是最底层
		private function set layer(val:int):void
		{
			if(MainManager.getInstance().getContainer(val)!=pyro.parent)
			{
				MainManager.getInstance().getContainer(val).addChild(pyro);
			}
		}
		//是否等待准备完毕，如果注册了这个事件，那么就无法触发complete事件
		private function set waitForReady(val:int):void
		{
			if(val==1)
			{
				pyro.addEventListener(Event.ENTER_FRAME,onEnter);
			}
		}
		private function onEnter(e:Event):void
		{
			if(pyro.netStream!=null && pyro.netStream.currentFPS>0)//当视频有帧频的时候，就表示视频准备完毕
			{
				this.dispatchEvent(new Event(Event.COMPLETE));
				MemoryRecovery.getInstance().gcFun(pyro,Event.ENTER_FRAME,onEnter);
				MainManager.getInstance().scriptManager.runScriptDirectly(this._readyScript);
			}
		}
		//设置视频播放完毕的脚本，这样可以设置准备完毕的时候的一些操作
		private function set readyScript(val:String):void
		{
			_readyScript=val;
		}		
		//设置视频播放完毕的脚本，这样可以设置视频播放完毕时，自动跳转到其他场景
		private function set completeScript(val:String):void
		{
			_completeScript=val;
			pyro.addEventListener(PyroEvent.COMPLETED,on_play_complete);
		}
		private function on_play_complete(e:PyroEvent):void
		{
			MainManager.getInstance().scriptManager.runScriptDirectly(this._completeScript);
			MemoryRecovery.getInstance().gcFun(pyro,PyroEvent.COMPLETED,on_play_complete);
			MemoryRecovery.getInstance().gcFun(pyro,PyroEvent.COMPLETED,on_play_complete);
			this.dispatchEvent(new Event(Event.COMPLETE));//抛出完毕的事件
		}
		private function waitForPlayComplete(val:int):void
		{
			if(val==1)
			{
				pyro.addEventListener(PyroEvent.COMPLETED,on_play_complete);
			}
		}
	}
}